<template>
	<view class="chat">
		<scroll-view  :style="{height: `${windowHeight-inputHeight}rpx`}"
		id="scrollview"
		scroll-y="true" 
		:scroll-top="scrollTop"
		:scroll-with-animation="true"
		class="scroll-view"
		>
			<!-- 提示 -->
			<view class="prompt">当前沟通的职位:{{position}}</view>
			<!-- 聊天页面 -->
			<view id="msglistview" class="chat-body">
				<!-- 聊天记录 -->
				<view v-for="(item,index) in msgList" :key="index">
					<!-- 客服发的消息 -->
					<view class="item Ai" v-if="item.botContent != ''">
						<!-- 客服头像 -->
						<view class="avatar left">
							<image src="/static/page1.jpg" mode="widthFix"></image>
						</view>
						<view class="message">
							<!-- 客服昵称 -->
						<view class="botname">{{item.botname}}</view>
						<!-- 客服消息内容 -->
						<view class="content left">{{item.botContent}}</view>
						</view>
					</view>
					<!-- 用户发的消息 -->
					<view class="item self" v-if="item.userContent != ''">
						<view class="message">
						<!-- 用户昵称 -->
						<view class="username">{{item.username}}</view>
						<!-- 用户信息内容 -->
						<view class="content right">{{item.userContent}}</view>
						</view>
						<!-- 用户头像 -->
						<view class="avatar right">
							<image src="/static/page1.jpg" mode="widthFix"></image>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
		<!-- 底部消息发送栏 -->
		<!-- 用来占位，防止聊天消息被发送框遮挡 -->
		<view class="chat-bottom" :style="{height: `${inputHeight}rpx`}">
			<view class="send-msg" :style="{bottom:`${keyboardHeight}rpx`}">
                <view class="uni-textarea">
					<textarea v-model="chatMsg"
					  maxlength="300"
					  confirm-type="send"
					  @confirm="handleSend"
                      :adjust-position="false"
					  :show-confirm-bar="false"
					  @linechange="sendHeight"
					  @focus="focus" @blur="blur"
					 auto-height  placeholder="我想问一下"></textarea>
					<uni-icons @click="handleSend" class="send-btn" custom-prefix="custom-icon" type="paperplane-filled" size="30"></uni-icons>
				</view>
				<view class="ability">
					<uni-icons custom-prefix="custom-icon" type="hand-up" size="30"></uni-icons>
					<uni-icons custom-prefix="custom-icon" type="image" size="30"></uni-icons>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {	
				position:'java',
				//键盘高度
				keyboardHeight:0,
		        // 聊天框距离底部的高度
		        bottomHeight: 0,
				//滚动距离
				scrollTop: 0,
				userId:'',
				//发送的消息
				chatMsg:"",
				msgList:[
					{
					    botContent: "今天又背了一个锅",
					    recordId: 0,
					    titleId: 0,
					    userContent: "",
					    userId: 0,
						botname: "产品小张@"
					},
					{
					    botContent: "",
					    recordId: 0,
					    titleId: 0,
					    userContent: "哈哈哈哈背锅是产品汪的工作之一嘛",
					    userId: 0,
						username: "产品小贝@"
					},
					{
					    botContent: "今天又背了一个锅",
					    recordId: 0,
					    titleId: 0,
					    userContent: "",
					    userId: 0,
						botname: "产品小张@"
					},
					{
					    botContent: "",
					    recordId: 0,
					    titleId: 0,
					    userContent: "哈哈哈哈背锅是产品汪的工作之一嘛",
					    userId: 0,
						username: "产品小贝@"
					},
				]
			}
		},
		updated(){
					//页面更新时调用聊天消息定位到最底部
					this.scrollToBottom();
				},
				computed: {
					windowHeight() {
					    return this.rpxTopx(uni.getSystemInfoSync().windowHeight)
					},
					// 键盘弹起来的高度+发送框高度
					inputHeight(){
						return this.bottomHeight+this.keyboardHeight
					}
				},
				onLoad(){
					uni.onKeyboardHeightChange(res => {
						//这里正常来讲代码直接写
						//this.keyboardHeight=this.rpxTopx(res.height)就行了
						//但是之前界面ui设计聊天框的高度有点高,为了不让键盘和聊天输入框之间距离差太大所以我改动了一下。
						this.keyboardHeight = this.rpxTopx(res.height-30)
						if(this.keyboardHeight<0)this.keyboardHeight = 0;
					})
				},
				onUnload(){
					uni.offKeyboardHeightChange()
				},
				methods: {
					focus(){
						this.scrollToBottom()
					},
					blur(){
						this.scrollToBottom()
					},
					// px转换成rpx
					rpxTopx(px){
						let deviceWidth = wx.getSystemInfoSync().windowWidth
						let rpx = ( 750 / deviceWidth ) * Number(px)
						return Math.floor(rpx)
					},
					// 监视聊天发送栏高度
					sendHeight(){
						setTimeout(()=>{
							let query = uni.createSelectorQuery();
							query.select('.send-msg').boundingClientRect()
							query.exec(res =>{
								this.bottomHeight = this.rpxTopx(res[0].height)
							})
						},10)
					},
					// 滚动至聊天底部
					scrollToBottom(e){
						setTimeout(()=>{
							let query = uni.createSelectorQuery().in(this);
							query.select('#scrollview').boundingClientRect();
							query.select('#msglistview').boundingClientRect();
							query.exec((res) =>{
								if(res[1].height > res[0].height){
									this.scrollTop = this.rpxTopx(res[1].height - res[0].height)
								}
							})
						},15)
					},
					// 发送消息
					handleSend() {
						//如果消息不为空
						if(!this.chatMsg||!/^\s+$/.test(this.chatMsg)){
							let obj = {
								botContent: "",
								recordId: 0,
								titleId: 0,
								userContent: this.chatMsg,
								userId: 0,
								username: "用户"
							}
							this.msgList.push(obj);
							this.chatMsg = '';
							this.scrollToBottom()
						}else {
							this.$modal.showToast('不能发送空白消息')
						}
					},
				}
			}
</script>
<style lang="scss" scoped>
	
	$chatContentbgc: #C2DCFF;
	$sendBtnbgc: #4F7DF5;
	
	view,button,text,input,textarea {
		margin: 0;
		padding: 0;
		box-sizing: border-box;
	}

	/* 聊天消息 */
	.chat {
		.scroll-view {
			::-webkit-scrollbar {
					    display: none;
					    width: 0 !important;
					    height: 0 !important;
					    -webkit-appearance: none;
					    background: transparent;
					    color: transparent;
					  }
			
			// background-color: orange;
			background-color: #F6F6F6;
			
			/* 提示 */
			.prompt {
				position: relative;
				top: 28rpx;
				left: 40rpx;
				width: 660rpx;
				height: 74rpx;
				border: 2rpx solid #bbbbbb;
				box-shadow: 0px 2rpx 10rpx 0rpx rgba(0, 0, 0, 0.4);
				text-align: center;
				font-size: 28rpx;
				line-height: 74rpx;
				margin-bottom: 20rpx;
			}
			
			
			.chat-body {
				display: flex;
				flex-direction: column;
				padding-top: 23rpx;
				// background-color:skyblue;
				
				.self {
					justify-content: flex-end;
				}
				.item {
					display: flex;
					padding: 23rpx 30rpx;
					
					.right {
						background-color: $chatContentbgc;
					}
					.left {
						background-color: #FFFFFF;
					}
                    // 聊天消息的三角形
					.right::after {
						position: absolute;
						display: inline-block;
						content: '';
						width: 0;
						height: 0;
						left: 100%;
						top: 10px;
						border: 12rpx solid transparent;
						border-left: 12rpx solid $chatContentbgc;
					}

					.left::after {
						position: absolute;
						display: inline-block;
						content: '';
						width: 0;
						height: 0;
						top: 10px;
						right: 100%;
						border: 12rpx solid transparent;
						border-right: 12rpx solid #FFFFFF;
					}

					.content {
						position: relative;
						max-width: 486rpx;
						border-radius: 8rpx;
						word-wrap: break-word;
						padding: 24rpx 24rpx;
						margin: 0 24rpx;
						border-radius: 5px;
						font-size: 32rpx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #333333;
						line-height: 42rpx;
					}

					.avatar {
						display: flex;
						justify-content: center;
						width: 100rpx;
						height: 100rpx;
						border-radius: 50rpx;
						margin-top: 30rpx;
						overflow: hidden;
						
						image {
							align-self: center;
						}

					}
					.botname,.username{
						padding: 2rpx 14rpx;
						margin: 0 14rpx;
						font-size: 24rpx;
					}
					.username{
						text-align: end;
					}
					
				}
			}
		}

		/* 底部聊天发送栏 */
		.chat-bottom {
			width: 100%;
			height: 177rpx;
			background: #F4F5F7;
			transition: all 0.1s ease;
			

			.send-msg {
				display: flex;
				align-items: flex-end;
				padding: 16rpx 30rpx;
				width: 100%;
				min-height: 177rpx;
				position: fixed;
				bottom: 0;
				background: #FFFFFF;
				transition: all 0.1s ease;
			}

			.uni-textarea {
				display: flex;
				flex-direction: row;
				justify-content: flex-start;
				padding-bottom: 70rpx;
                
				textarea {
					width: 400rpx;
					min-height: 75rpx;
					max-height: 500rpx;
					background: #FFFFFF;
					border-top-left-radius: 8rpx;
					border-bottom-left-radius: 8rpx;
					font-size: 32rpx;
					font-family: PingFang SC;
					color: #333333;
					line-height: 43rpx;
					padding: 5rpx 8rpx;
					border: 2rpx solid #BBBBBB;
					border-right: 0rpx;
				}
			.send-btn {
				display: flex;
				align-items: center;
				justify-content: center;
				width: 128rpx;
				height: 72rpx;
				background: #FFFFFF;
				border-top-right-radius: 8rpx;
				border-bottom-right-radius: 8rpx;
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #FFFFFF;
				line-height: 28rpx;
				border: 2rpx solid #BBBBBB;
				border-left: 0rpx;
			}
			}
            
			.ability{
				display: flex;
				flex-direction: row;
				justify-content: space-around;
				padding-bottom: 70rpx;
				margin:0 20rpx;
			}
		}
		
	}
</style>
